using System;
using System.Collections.Generic;
using System.Text;

using System.Windows;
using System.Windows.Ink;
using WozLib.UI.Ink.SketchObjects;
using WozLib.WozCommands;
using System.Xml.Serialization;

namespace WozLib.UI.Ink.SketchCommands
{
  public class CreateSketchCommand : SketchCommand
  {
    [XmlElement("SelectToolWozCommand")]
    public SelectToolWozCommand SelectToolWozCommand
    {
      get
      {
        return _SelectToolWozCommand;
      }
      set
      {
        _SelectToolWozCommand = value;
      }
    } private SelectToolWozCommand _SelectToolWozCommand;

    public CreateSketchCommand()
    {
    }

    public CreateSketchCommand(SketchObject wozObject)
      : base(wozObject)
    {
    }

    public CreateSketchCommand(SketchObject wozObject, SelectToolWozCommand selectToolWozCommand)
      : base(wozObject)
    {
      SelectToolWozCommand = selectToolWozCommand;
    }

    public CreateSketchCommand(IEnumerable<SketchObject> wozObjects)
      : base(wozObjects)
    {
    }

    public override void Redo()
    {
      if (this.WozClientCommandReferences != null && this.WozClientCommandReferences.InkCanvas != null)
      {
        if (SelectToolWozCommand != null)
        {
          SelectToolWozCommand.Redo();
        }

        foreach (SketchObject wozObject in this.TargetWozObjects)
        {
          ISketchObject iWozObject = wozObject.GetIWozObject(this.WozClientCommandReferences.WozObjectRegistry);

          //todo
          // This will need to be slightly modified when we also have layers

          if (iWozObject.GetProperty(WozLib.UI.Ink.SketchProperties.WozPropertyType.BehindInk) == null)
          {

            if (iWozObject is UIElement)
            {
              if (!this.WozClientCommandReferences.InkCanvas.Children.Contains(iWozObject as UIElement))
              {
                this.WozClientCommandReferences.InkCanvas.Children.Add(iWozObject as UIElement);
              }
            }
            else if (iWozObject is Stroke)
            {
              if (!this.WozClientCommandReferences.InkCanvas.Strokes.Contains(iWozObject as Stroke))
              {
                this.WozClientCommandReferences.InkCanvas.Strokes.Add(iWozObject as Stroke);
              }
            }
          }
          else
          {
            if (iWozObject is UIElement && this.WozClientCommandReferences.InkOverlayCanvas != null)
            {
              if (!this.WozClientCommandReferences.InkOverlayCanvas.Children.Contains(iWozObject as UIElement))
              {
                this.WozClientCommandReferences.InkOverlayCanvas.Children.Add(iWozObject as UIElement);
              }
            }
            else if (iWozObject is UIElement && this.WozClientCommandReferences.InkOverlayCanvas == null)
            {
              if (!this.WozClientCommandReferences.InkCanvas.Children.Contains(iWozObject as UIElement))
              {
                this.WozClientCommandReferences.InkCanvas.Children.Add(iWozObject as UIElement);
              }
            }
          }
        }

        
      }
    }

    public override void Undo()
    {
      if (this.WozClientCommandReferences != null && this.WozClientCommandReferences.InkCanvas != null)
      {
        foreach (SketchObject wozObject in this.TargetWozObjects)
        {
          ISketchObject iWozObject = wozObject.GetIWozObject(this.WozClientCommandReferences.WozObjectRegistry);

          //todo
          // This will need to be slightly modified when we also have layers

          if (iWozObject.GetProperty(WozLib.UI.Ink.SketchProperties.WozPropertyType.BehindInk) == null)
          {
            if (iWozObject is UIElement)
            {
              if (this.WozClientCommandReferences.InkCanvas.Children.Contains(iWozObject as UIElement))
              {
                this.WozClientCommandReferences.InkCanvas.Children.Remove(iWozObject as UIElement);
              }
            }
            else if (iWozObject is Stroke)
            {
              if (this.WozClientCommandReferences.InkCanvas.Strokes.Contains(iWozObject as Stroke))
              {
                this.WozClientCommandReferences.InkCanvas.Strokes.Remove(iWozObject as Stroke);
              }
            }
          }
          else
          {
            if (iWozObject is UIElement && this.WozClientCommandReferences.InkOverlayCanvas != null)
            {
              if (this.WozClientCommandReferences.InkOverlayCanvas.Children.Contains(iWozObject as UIElement))
              {
                this.WozClientCommandReferences.InkOverlayCanvas.Children.Remove(iWozObject as UIElement);
              }
            }
            else if (iWozObject is UIElement && this.WozClientCommandReferences.InkOverlayCanvas == null)
            {
              if (this.WozClientCommandReferences.InkCanvas.Children.Contains(iWozObject as UIElement))
              {
                this.WozClientCommandReferences.InkCanvas.Children.Remove(iWozObject as UIElement);
              }
            }
          }
        }

        if (SelectToolWozCommand != null)
        {
          SelectToolWozCommand.Undo();
        }
      }
    }
  }
}
